package com.ys.everydaynews.ui.main.fragment


import android.util.Log
import androidx.fragment.app.Fragment
import com.ys.everydaynews.R
import com.ys.everydaynews.base.BaseFragment
import com.ys.everydaynews.databinding.FragmentHomeBinding
import com.ys.everydaynews.mvp.presenter.BasePresenter
import com.ys.everydaynews.mvp.view.BaseView
import com.ys.everydaynews.ui.main.adapter.MainTabListAdapter
import com.ys.everydaynews.ui.main.model.EventModel
import com.ys.everydaynews.ui.main.model.TabChModel
import com.ys.everydaynews.ui.main.model.TabListModel

class HomeFragment : BaseFragment<BaseView, BasePresenter<BaseView>, FragmentHomeBinding>() {
    private var fragmentList: MutableList<Fragment> = mutableListOf()
    private var titles: Array<String> = arrayOf()
    private var mainTabListAdapter: MainTabListAdapter? = null
    private var tabList: TabListModel? = null

    override fun initView() {
        titles = resources.getStringArray(R.array.min_title)
        for (i: Int in titles.indices) {
            fragmentList.add(MinListFragment())
        }
        if (mainTabListAdapter == null) {
            mainTabListAdapter = MainTabListAdapter(childFragmentManager, fragmentList, titles)
        } else {
            mainTabListAdapter!!.notifyDataSetChanged()
        }
        binding.vpMin.offscreenPageLimit = fragmentList.size
        binding.vpMin.adapter = mainTabListAdapter
        binding.stlTabTitle.setViewPager(binding.vpMin)
        binding.imgPd.setOnClickListener {
            val dialog = TabListDialogFragment(tabList!!)
            dialog.show(childFragmentManager, "dialog")
        }
    }

    override fun initData() {
        val myCh: MutableList<TabChModel> = mutableListOf(
            TabChModel("关注", 1),
            TabChModel("推荐", 2),
            TabChModel("小说", 3),
            TabChModel("科技", 4),
            TabChModel("军事", 5),
            TabChModel("音乐", 6),
            TabChModel("国际", 7)
        )
        val noCh: MutableList<TabChModel> = mutableListOf(
            TabChModel("人文", 8),
            TabChModel("历史", 9),
            TabChModel("游戏", 10),
            TabChModel("程序", 11),
            TabChModel("风景", 12),
            TabChModel("疫情", 13)
        )
        tabList = TabListModel(myCh, noCh)
        postMsg(EventModel(106, "名称"))
    }

    override fun isRegister(): Boolean = true
    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun createPersenter() = BasePresenter<BaseView>()

    override fun <T> setData(data: T) {

    }

    override fun setError(err: String) {
    }
}